package com.bobo.service.impl;

import com.bobo.service.CheckTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class CheckTokenServiceImpl implements CheckTokenService {
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public void checkSessionIdAndUrl(String key) {

        if(StringUtils.isEmpty(key)){
            throw new RuntimeException("key is null");//key null
        }
        //如果redis没有该key ,则粗惹存入redis并且设置超时时间2秒
        if(redisTemplate.opsForValue().get(key)==null){
            redisTemplate.opsForValue().set(key,key,2, TimeUnit.SECONDS);
        }else{
            throw new RuntimeException("重复提交了");//重复提交
        }
    }
}
